package com.kma.summer2012.client;

import com.google.gwt.core.client.GWT;
import com.kma.summer2012.client.constants.ConstantsPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: EUGENE
 * Date: 3/5/13
 * Time: 12:23 AM
 * To change this template use File | Settings | File Templates.
 */
public class SiteMap {
    public class Node {
        public String label;
        public String historyItem;

        public Node(String label, String historyItem) {
            this.label = label;
            this.historyItem = historyItem;
        }
    }

    private static SiteMap myself;
    private HashMap<String, String[]> siteMap = null;
    private HashMap<String, String> labels = null;
    private static ConstantsPresenter c = GWT.create(ConstantsPresenter.class);

    public static SiteMap getInstance() {
        if (myself == null) {
            myself = new SiteMap();
        }
        return myself;
    }

    private SiteMap() {
        this.createMap();
    }

    private void createMap() {
        siteMap = new HashMap<String, String[]>();
        labels = new HashMap<String, String>();

        siteMap.put(c.PASSWORD_RECOVERY(), new String[]{c.LOGIN(), c.PASSWORD_RECOVERY()});
        siteMap.put(c.CHANGE_PASSWORD(), new String[]{c.LOGIN(), c.CHANGE_PASSWORD()});
        siteMap.put(c.ADD_WORK(), new String[]{c.MANAGE_WORKS(), c.ADD_WORK()});
        siteMap.put(c.ADD_TOOL(), new String[]{c.MANAGE_TOOLS(), c.ADD_TOOL()});
        siteMap.put(c.ADD_CATEGORY(), new String[]{c.MANAGE_WORKS(), c.ADD_CATEGORY()});
        siteMap.put(c.EDIT_CATEGORY(), new String[]{c.MANAGE_WORKS(), c.EDIT_CATEGORY()});
        siteMap.put(c.EDIT_TOOL(), new String[]{c.MANAGE_TOOLS(), c.EDIT_TOOL()});
        siteMap.put(c.EDIT_WORK(), new String[]{c.MANAGE_WORKS(), c.EDIT_WORK()});
        siteMap.put(c.CONFIRM_PASSWORD(), new String[]{c.LOGIN(), c.CONFIRM_PASSWORD()});
        siteMap.put(c.SUCCESSFULL_REJECT(), new String[]{c.REJECT(), c.SUCCESSFULL_REJECT()});
        siteMap.put(c.ASSIGN_PAGE(), new String[]{c.EDIT_REQUEST_MANAGER(), c.ASSIGN_PAGE()});


        labels.put(c.LOGIN(), "Login Page");
        labels.put(c.PASSWORD_RECOVERY(), "Recover Password");
        labels.put(c.CHANGE_PASSWORD(), "Change Password");
        labels.put(c.MANAGE_TOOLS(), "Manage Tools");
        labels.put(c.MANAGE_WORKS(), "Manage Works");
        labels.put(c.ADD_WORK(), "Add Work");
        labels.put(c.ADD_TOOL(), "Add Tool");
        labels.put(c.ADD_CATEGORY(), "Add Category");
        labels.put(c.EDIT_CATEGORY(), "Edit Category");
        labels.put(c.EDIT_TOOL(), "Edit Tool");
        labels.put(c.EDIT_WORK(), "Edit Work");
        labels.put(c.CONFIRM_PASSWORD(), "Password Confirmation");
        labels.put(c.SUCCESSFULL_REJECT(), "Successful Reject");
        labels.put(c.REJECT(), "Reject Requesrt Page");
        labels.put(c.ASSIGN_PAGE(), "Assign Request Page");
        labels.put(c.CLOSE_REQUEST(), "Close Request Page");
        labels.put(c.VIEW_MAP(), "Request Map");
        labels.put(c.MAIN_PAGE(), "Main");
        labels.put(c.CREATE_REQUEST(), "Create Request");
        labels.put(c.EDIT_REQUEST_OPERATOR(), "Edit Request");
        labels.put(c.EDIT_REQUEST_MANAGER(), "Edit Request");
        labels.put(c.SHOW_OPERATOR_REQUEST(), "Request Details");
        labels.put(c.SHOW_ENGINEER_REQUEST(), "Request Details");
        labels.put(c.SHOW_MANAGER_REQUEST(), "Request Details");
    }

    public List<Node> getPath(String historyItem) {
        List<Node> path = new ArrayList<Node>();
        if (siteMap.get(historyItem) != null) {
            String[] historyPath = siteMap.get(historyItem);
            for (String hitem : historyPath) {
                path.add(new Node(labels.get(hitem), hitem));
            }
            String firstNodeStr = historyPath[0];
            String[] deeperPath = siteMap.get(firstNodeStr);
            while (deeperPath != null) {
                String hitem = "";
                for (int i = deeperPath.length - 1; i > 0; --i) {
                    hitem = labels.get(deeperPath[i]);
                    path.add(0, new Node(labels.get(hitem), hitem));
                }
                if (deeperPath.length > 0) {
                    deeperPath = siteMap.get(hitem);
                } else {
                    deeperPath = null;
                }
            }
        } else {
            if (!historyItem.equals(c.MAIN_PAGE()))
                path.add(0, new Node(labels.get(historyItem), historyItem));
        }
        path.add(0, new Node(labels.get(c.MAIN_PAGE()), c.MAIN_PAGE()));
        return path;
    }
}
